package handlers

import (
	"chirp-read/cache"
	"chirp-read/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func CourierAdd(c *gin.Context) {
	courier_code := c.PostForm("courier_code")
	courier_name := c.PostForm("courier_name")
	courier_phone := c.PostForm("courier_phone")
	if courier_code == "" || courier_name == "" || courier_phone == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Courier{
		Model:        gorm.Model{},
		CourierCode:  courier_code,
		CourierName:  courier_name,
		CourierPhone: courier_phone,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func CourierOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_to := c.PostForm("order_to")
	courier_id, _ := strconv.Atoi(c.PostForm("courier_id"))
	if order_code == "" || order_to == "" || courier_id == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.CourOrder{
		Model:     gorm.Model{},
		OrderCode: order_code,
		OrderTo:   order_to,
		CourierId: courier_id,
	}
	err := models.DB.Where("courier_id = ?", courier_id).Find(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询配送员失败",
		})
		return
	}
	if data.ID > 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "已有配送员",
		})
		return
	}
	err = models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CourierOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var courierOrder []models.CourierOrderList
	redis_key := "courier_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cour_orders").
			Select("cour_orders.id as OrderId",
				"cour_orders.order_code as OrderCode",
				"cour_orders.order_to as OrderTo",
				"couriers.courier_name as CourierName",
				"couriers.courier_phone as CourierPhone",
			).
			Joins("left join couriers on couriers.id=cour_orders.couriers.id").
			Offset(offset).Limit(size).Find(&courierOrder)
		jsonData, _ := json.Marshal(courierOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &courierOrder)
	}
	var total int64
	models.DB.Model(&models.GoodOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  courierOrder,
		"total": total,
	})
}

func CourierOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var courierOrder models.CourierOrderList
	redis_key := "courier_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cour_orders").
			Select("cour_orders.id as OrderId",
				"cour_orders.order_code as OrderCode",
				"cour_orders.order_price as OrderPrice",
				"couriers.courier_name as CourierName",
				"couriers.courier_desc as CourierDesc",
				"couriers.courier_type as CourierType",
			).
			Joins("left join couriers on cour_orders.courier_id=couriers.id").
			Where("couriers.id = ?", id).Find(&courierOrder)
		jsonData, _ := json.Marshal(courierOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &courierOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": courierOrder,
	})
}
